<template>
	<view class="goods">
		<uni-nav-bar :fixed="true" left-icon="back" :border="false" backgroundColor="#f9f2ea" leftText="收货地址"
			:statusBar="true" @clickLeft="goBack"></uni-nav-bar>
		<view class="nomore" v-if="list.length == 0">
			请添加收货地址
		</view>
		<view class="address-item flex-between" v-for="(item, index) in list" :key="index">
			<view class="content" @click="chooseAddress(item)">
				<view class="flex">
					<view class="name">
						{{item.name}}
					</view>
					<view class="phone">
						{{item.phone}}
					</view>
				</view>
				<view class="detail flex">
					<text class="text" v-show="item.is_default == 2">默认</text>
					<text>{{item.area+item.address}}</text>
				</view>
			</view>
			<view class="line">

			</view>
			<view class="edit" @click="toDetails(item)">
				<image class="img" src="../../static/edit.png" mode=""></image>
			</view>
		</view>
		<view class="add" @click="toDetails({id:-1})">
			添加新地址
		</view>
	</view>
</template>

<script>
	import {
		addressList
	} from "@/api/public.js"
	export default {
		data() {
			return {
				list: [],
				num: 0
			}
		},
		onLoad(obj) {
			this.num = obj.num;
			console.log(this.num)
			this.getList();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			getList() {
				addressList().then(res => {
					this.list = res.reData.data;
					if (uni.getStorageSync("approveAddress")) {
						var address = JSON.parse(uni.getStorageSync("approveAddress"));
						for (let i in this.list) {
							if (this.list[i].is_default == 2) {
								if (this.list[i].id == address.id) {

								} else {
									uni.setStorageSync("approveAddress", JSON.stringify(this.list[i]))
								}
							}
						}
					} else {
						for (let i in this.list) {
							if (this.list[i].is_default == 2) {
								uni.setStorageSync("approveAddress", JSON.stringify(this.list[i]))
							}
						}
					}

				})
			},
			chooseAddress(item) {
				if (this.num == 1) {
					uni.navigateTo({
						url: "confirmOrder?address=" + JSON.stringify(item)
					})
				}
			},
			toDetails(item) {
				uni.navigateTo({
					url: "addAddress?data=" + JSON.stringify(item)
				})


			},
		}
	}
</script>

<style lang="scss" scoped>
	.goods {
		padding: 0 30rpx 100rpx;
	}

	.address-item {
		padding: 30rpx;
		background: #fffaf5;
		box-shadow: 2px 2px 4px rgba(186, 162, 138, 0.18);
		align-items: center;
		border-radius: 20rpx;
		margin-bottom: 20rpx;

		.content {
			flex-grow: 1;

			.name {
				font-size: 28rpx;
				line-height: 36rpx;
				margin-right: 30rpx;
			}

			.phone {
				margin-top: 10rpx;
				font-size: 22rpx;
				line-height: 24rpx;
				color: #666;
			}

			.detail {
				.text {
					padding: 4rpx 10rpx;
					margin-right: 10rpx;
					background: #fff4f0;
					color: #d45825;
					width: 70rpx;
					height: 30rpx;
				}

				margin-top: 10rpx;
				font-size: 26rpx;
				line-height: 32rpx;
				color: #333;
			}

		}

		.line {
			height: 80rpx;
			width: 4rpx;
			background: #f6f4f5;
			margin-left: 50rpx;
		}

		.edit {
			font-size: 28rpx;
			color: #666;
			line-height: 100rpx;
			padding: 0 20rpx 0 30rpx;

			.img {
				width: 44rpx;
				height: 44rpx;
			}
		}
	}

	.add {
		position: fixed;
		width: 180rpx;
		height: 72rpx;
		border-radius: 36rpx;
		left: 30rpx;
		bottom: 60rpx;
		background: rgba(214, 142, 127, 1);
		box-shadow: 5px 5px 10px rgba(189, 185, 166, 0.5);
		color: #fff;
		font-size: 26rpx;
		line-height: 72rpx;
		text-align: center;
		transform: translate(-50%);
		left: 50%;
	}

	.nomore {
		text-align: center;
		line-height: 64rpx;
	}
</style>
